 
const path = require('path');//引入path模块
const autoprefixer = require('autoprefixer');
const pxtorem = require('postcss-pxtorem');

const CompressionWebpackPlugin = require("compression-webpack-plugin");
const productionGzipExtensions = /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i;

function resolve(dir){
     return path.join(__dirname,dir)//path.join(__dirname)设置绝对路径
 }

module.exports = {
	css: {
		loaderOptions: {
			postcss: {
				plugins: [
					autoprefixer(),
					pxtorem({
						rootValue: 37.5,
						propList: ['*']
					})
				]
			}
		}
	},
	configureWebpack: config => {
		if (process.env.NODE_ENV === 'production') {
			// 为生产环境修改配置...
			config.mode = 'production'
			// 将每个依赖包打包成单独的js文件
			let optimization = {
			  runtimeChunk: 'single',
			  splitChunks: {
				chunks: 'all',
				maxInitialRequests: Infinity,
				minSize: 20000,
				cacheGroups: {
				  vendor: {
					test: /[\\/]node_modules[\\/]/,
					name (module) {
					  // get the name. E.g. node_modules/packageName/not/this/part.js
					  // or node_modules/packageName
					  const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1]
					  // npm package names are URL-safe, but some servers don't like @ symbols
					  return `npm.${packageName.replace('@', '')}`
					}
				  }
				}
			  }
			}
			Object.assign(config, {
			  optimization
			})
		} else {
			// 为开发环境修改配置...
			config.mode = 'development'
		}



        const plugins = [];

        // Begin 生成 gzip 压缩文件
        plugins.push(
            new CompressionWebpackPlugin({
                filename: "[path].gz[query]",
                algorithm: "gzip",
                test: productionGzipExtensions,
                threshold: 10240,
                minRatio: 0.8
            })
        );
        // End 生成 gzip 压缩文件
        
        config.plugins = [...config.plugins, ...plugins];
    },
	chainWebpack:(config)=>{
		config.resolve.alias
		.set('@',resolve('./src'))
		.set('components',resolve('./src/components'))
		//set第一个参数：设置的别名，第二个参数：设置的路径
　　　　
	}
}